package com.boruan.qq.zxgylibrary.utils;

import android.content.Context;
import android.widget.ImageView;

import com.boruan.qq.zxgylibrary.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

/**
 * Created by 崔琦 on 2019/5/20.
 * des: ....
 */

public class GlideUtil {

    ImageView imageView;
    private DiskCacheStrategy diskCache = DiskCacheStrategy.ALL;//磁盘缓存(缓存所有版本的图像)
    private boolean isSkipMemoryCache = true;//跳过内存缓存
    /** 加载时的占位符,不带圆角 */
//    RequestOptions requestOptions = new RequestOptions().placeholder(R.mipmap.ic_launcher_round);
//    /** 加载时的占位符,带圆角 */
//    RoundedCorners roundedCorners= new RoundedCorners(10);
//    //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//    RequestOptions requestOptionsCircle = new RequestOptions().placeholder(R.mipmap.ic_launcher_round).bitmapTransform(roundedCorners);


    public GlideUtil attach(ImageView imageView) {
        this.imageView = imageView;
        return this;
    }


    public GlideUtil injectImage(String url){
        Glide.with(imageView.getContext())//可以传入activity，fragment。
                .load(url)//加载完整的url
                .centerCrop()//图片会填充整个imageview
                .diskCacheStrategy(diskCache)//磁盘缓存
                .skipMemoryCache(isSkipMemoryCache)//内存缓存true为跳过，默认为false，全部缓存到内存。
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)
//                .apply(requestOptions)//加载图片时等待的图片(占位)
                .into(imageView);
        return this;
    }

    public GlideUtil injectImageAsBitmap(String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .centerCrop()
                .diskCacheStrategy(diskCache)
                .skipMemoryCache(false)//图片全部缓存到内存
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)
                .into(imageView);
        return this;
    }

    public GlideUtil injectImageWithNull(String url) {
        Glide.with(imageView.getContext())
                .load(url)
                .centerCrop()
                .diskCacheStrategy(diskCache)
                .skipMemoryCache(false)//图片全部缓存到内存
                .placeholder(R.mipmap.drug_loading_logo)
                .error(R.mipmap.drug_loading_logo)
                .into(imageView);
        return this;
    }

    public GlideUtil loadRectangleWithNull(String url,final Context context){

        Glide.with(imageView.getContext())
                .load(url)
                .diskCacheStrategy(diskCache) //设置缓存
                .skipMemoryCache(false)//图片全部缓存到内存
                .placeholder(R.mipmap.ic_launcher_round)
                .error(R.mipmap.ic_launcher_round)
                .into(imageView);
        return this;
    }

}
